<?php
require_once(dirname(__FILE__) . '/app.php');

if(!$INI['db']['host']) redirect( WEB_ROOT . '/install.php' );

$request_uri = 'index';
//$team = current_team($city['id']);
//if ($team) {
//	$_GET['id'] = abs(intval($team['id']));
//	die(require_once('team.php'));
//}
//
//include template('subscribe');

/* find teams */

/* find teams category */
($group = strval($_GET['group'])) || ($group = 'all');

$condition = array('zone' => 'group');
$count = Table::Count('category', $condition);
$categories = DB::LimitQuery('category', array(
	'condition' => $condition,
	'order' => 'ORDER BY sort_order DESC',
));
/* End */

/* find current and past teams */
$today = strtotime(date('Y-m-d'));
$condition = array(
	'city_id' => array(0, abs(intval($city['id']))),
	"begin_time <= {$today}",
	"end_time > {$today}",
);

// 类别处理
if ( $group && $group != 'all' ) {
	foreach($categories AS $id=>$one){
		if($one['ename'] == $group){
			$condition['group_id'] = abs(intval($one['id']));
			break;
		}
	}
}

if (!option_yes('displayfailure')) {
	$condition['OR'] = array(
		"now_number >= min_number",
		"end_time > '{$daytime}'",
	);
}

// 分页处理
$count = Table::Count('team', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 10);
$teams = DB::LimitQuery('team', array(
	'condition' => $condition,
	'order' => 'ORDER BY begin_time DESC, id DESC',
	'size' => $pagesize,
	'offset' => $offset,
));

// add teams other info
foreach($teams AS $index=>$one){
	team_state($one);
	if ($one['state']=='none') $one['picclass'] = 'isopen';
	if ($one['state']=='soldout') $one['picclass'] = 'soldout';
	
	/* get partner info */
	$one['partner'] = Table::Fetch('partner', $one['partner_id']);
	/* End */
	
	/* get tatics */
	$min_discount = 10;
	$max_discount = 0;
	$tatics = explode('|', $one['tatics']);
	foreach($tatics as $sid=>$t){
		$tatics[$sid] = explode(',', $t);
		$discountTmp = explode('-', $tatics[$sid][2]);
		if(($user_network_weights['total'] >= $tatics[$sid][0])){
			if($tatics[$sid][1] == 0){
				$one['discount_rate'] = $tatics[$sid][2];
			} elseif($user_network_weights['total'] < $tatics[$sid][1]){
				if($user_network_weights['total'] == 0){
					$one['discount_rate'] = $discountTmp[1];
				} else{
					// 低折扣+（高折扣-低折扣）/（高影响力-低影响力）*（个人影响力-低影响力）
					$one['discount_rate'] = round($discountTmp[0] + (($discountTmp[1] - $discountTmp[0])/($tatics[$sid][1] - $tatics[$sid][0]))*($user_network_weights['total'] - $tatics[$sid][0]), 1);
				}
			}
		}
		if($min_discount > $discountTmp[0]){
			$min_discount = $discountTmp[0];
		}
		if($max_discount < $discountTmp[1]){
			$max_discount = $discountTmp[1];
		}
	}
	$one['min_discount'] = $min_discount;
	$one['max_discount'] = $max_discount;
	/* End */
	
	$teams[$index] = $one;
}
/* End */

/* 只有一个活动项目 */
//if(count($teams) == 1){
//	redirect(WEB_ROOT.'/team.php?id='.$teams[0]['id']);
//}
/* End */

/* find future teams */
$condition = array(
	'city_id' => array(0, abs(intval($city['id']))),
	"begin_time > {$today}",
);

// 类别处理
if ( $group && $group != 'all' ) {
	foreach($categories AS $id=>$one){
		if($one['ename'] == $group){
			$condition['group_id'] = abs(intval($one['id']));
			break;
		}
	}
}

if (!option_yes('displayfailure')) {
	$condition['OR'] = array(
		"now_number >= min_number",
		"end_time > '{$daytime}'",
	);
}

// 分页处理
$count = Table::Count('team', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 10);
$future_teams = DB::LimitQuery('team', array(
	'condition' => $condition,
	'order' => 'ORDER BY begin_time DESC, id DESC',
	//'size' => $pagesize,
	//'offset' => $offset,
));

// add teams other info
foreach($future_teams AS $index=>$one){
	team_state($one);
	if ($one['state']=='none') $one['picclass'] = 'isopen';
	if ($one['state']=='soldout') $one['picclass'] = 'soldout';
	
	/* get partner info */
	$one['partner'] = Table::Fetch('partner', $one['partner_id']);
	/* End */
	
	/* get tatics */
	$min_discount = 10;
	$max_discount = 0;
	$tatics = explode('|', $one['tatics']);
	foreach($tatics as $sid=>$t){
		$tatics[$sid] = explode(',', $t);
		$discountTmp = explode('-', $tatics[$sid][2]);
		if(($user_network_weights['total'] >= $tatics[$sid][0])){
			if($tatics[$sid][1] == 0){
				$one['discount_rate'] = $tatics[$sid][2];
			} elseif($user_network_weights['total'] < $tatics[$sid][1]){
				if($user_network_weights['total'] == 0){
					$one['discount_rate'] = $discountTmp[1];
				} else{
					// 低折扣+（高折扣-低折扣）/（高影响力-低影响力）*（个人影响力-低影响力）
					$one['discount_rate'] = round($discountTmp[0] + (($discountTmp[1] - $discountTmp[0])/($tatics[$sid][1] - $tatics[$sid][0]))*($user_network_weights['total'] - $tatics[$sid][0]), 1);
				}
			}
		}
		if($min_discount > $discountTmp[0]){
			$min_discount = $discountTmp[0];
		}
		if($max_discount < $discountTmp[1]){
			$max_discount = $discountTmp[1];
		}
	}
	$one['min_discount'] = $min_discount;
	$one['max_discount'] = $max_discount;
	/* End */
	
	$future_teams[$index] = $one;
}
/* End */

$pagetitle = $INI['system']['abbreviation'] . '特惠';

include template('index_list');